package com.yc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yc.Result;
import com.yc.entities.BloodSugarDepartment;
import com.yc.entities.BloodSugarDoctor;
import com.yc.service.BloodSugarDepartmentService;
import com.yc.service.BloodSugarDoctorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 医生信息表 前端控制器
 * </p>
 *
 * @author kuakha
 * @since 2021-04-07
 */
@Api(value = "bloodSugarDoctor", tags = {"医生相关接口"})
@RestController
@RequestMapping("/bloodSugarDoctor")
public class BloodSugarDoctorController {

    @Autowired
    private BloodSugarDoctorService bloodSugarDoctorService;

    @Autowired
    private BloodSugarDepartmentService bloodSugarDepartmentService;

    @ApiOperation(value = "APP查看所有医生，根据院长id和科室id 二级联动")
    @PostMapping("getAllDoctorByDepartmentIdAndDeanId")
    public Result getAllDoctorByDepartmentIdAndDeanId(@ApiParam(value = "type=1：查看科室  type=2：查看科室下的医生")@RequestParam(required = true) String type,
                                                      @ApiParam(value = "科室Id")@RequestParam(required = false) String departmentId, @ApiParam(value = "院长Id")@RequestParam(required = false)String deanId,
                                                      @ApiParam(value = "医生Id")@RequestParam(required = false)String doctorId, @ApiParam(value = "权限Id")@RequestParam(required = false)String perId) {
        if (StringUtils.isEmpty(type)) {
            return Result.error().code(301).message("查找类型不能为空");
        }
        if (type.equals("1")) {
            if (StringUtils.isEmpty(deanId)) {
                return Result.error().code(302).message("院长Id不能为空");
            }
            if (StringUtils.isEmpty(perId)) {
                return Result.error().code(302).message("权限Id不能为空");
            }
            if (StringUtils.isEmpty(doctorId)) {
                return Result.error().code(302).message("当前登录医生Id不能为空");
            }
            List<BloodSugarDepartment> departmentByPerIdAndDocId = bloodSugarDepartmentService.getDepartmentByPerIdAndDocId(perId, deanId, doctorId);
            if (CollectionUtils.isNotEmpty(departmentByPerIdAndDocId)) {
                return Result.ok().data(departmentByPerIdAndDocId);
            } else {
                return Result.ok().code(300).message("暂无数据");
            }
        } else if (type.equals("2")) {
            if (StringUtils.isEmpty(departmentId)) {
                return Result.error().code(303).message("科室Id不能为空");
            }
            if (StringUtils.isEmpty(deanId)) {
                return Result.error().code(302).message("院长Id不能为空");
            }
            QueryWrapper<BloodSugarDoctor> doctorQuery = Wrappers.query();
            doctorQuery.eq("dean_id", deanId);
            doctorQuery.eq("department_id", departmentId);
            List<BloodSugarDoctor> allDoctorBydepIdAndDeanId = bloodSugarDoctorService.list(doctorQuery);
            if (CollectionUtils.isNotEmpty(allDoctorBydepIdAndDeanId)) {
                return Result.ok().data(allDoctorBydepIdAndDeanId);
            } else {
                return Result.ok().code(300).message("暂无数据");
            }
        } else {
            return Result.error().code(299).message("查找类型不匹配");
        }
    }
}

